package com.qujia.fleet.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qujia.fleet.domain.FleetCard;
import com.qujia.fleet.domain.FleetCardRechargeRecord;
import com.qujia.fleet.domain.FleetCardSub;
import com.qujia.fleet.domain.dto.FleetCardRechargeRecordDto;
import com.qujia.fleet.domain.form.FleetCardQueryForm;
import com.qujia.fleet.domain.form.FleetCardRechargeRecordQueryForm;
import com.qujia.fleet.domain.vo.FleetCardRechargeRecordVo;
import com.qujia.fleet.domain.vo.FleetCardRecordBalanceVo;
import com.qujia.fleet.domain.vo.FleetCardVo;
import com.qujia.fleet.enums.CardRechargeRecordEnum;
import com.qujia.fleet.mapper.FleetCardRechargeRecordMapper;
import com.qujia.fleet.service.FleetCardRechargeRecordService;
import com.qujia.fleet.service.FleetCardService;
import com.qujia.fleet.service.FleetCardSubService;
import com.qujia.station.domain.Station;
import com.qujia.station.service.IStationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class FleetCardRechargeRecordServiceImpl implements FleetCardRechargeRecordService {

    @Autowired
    private FleetCardRechargeRecordMapper fleetCardRechargeRecordMapper;

    @Autowired
    private FleetCardService fleetCardService;

    @Autowired
    private FleetCardSubService fleetCardSubService;

    @Autowired
    private IStationService stationService;

    @Override
    public int insertFleetCardRechargeRecord(FleetCardRechargeRecord fleetCardRechargeRecord) {
        return fleetCardRechargeRecordMapper.insertFleetCardRechargeRecord(fleetCardRechargeRecord);
    }

    @Override
    public List<FleetCardRechargeRecordVo> selectFleetCardRechargeRecordList(FleetCardRechargeRecordQueryForm fleetCardRechargeRecordQueryForm) {
        return fleetCardRechargeRecordMapper.selectFleetCardRechargeRecordList(fleetCardRechargeRecordQueryForm);
    }

    @Override
    public List<FleetCardRecordBalanceVo> selectFleetCardRecordBalance(FleetCardRechargeRecordDto fleetCardRechargeRecordDto) {
        return fleetCardRechargeRecordMapper.selectFleetCardRecordBalance(fleetCardRechargeRecordDto);
    }

    @Override
    public FleetCardRecordBalanceVo selectFleetCardRecordStatistics(FleetCardRechargeRecordDto fleetCardRechargeRecordDto) {
        log.info("selectFleetCardRecordStatistics请求参数：[{}]", JSONObject.toJSONString(fleetCardRechargeRecordDto));
        List<FleetCardRecordBalanceVo> list = fleetCardRechargeRecordMapper.selectFleetCardRecordBalance(fleetCardRechargeRecordDto);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }

        //车队卡消费总计
        BigDecimal consumptionBalance = BigDecimal.ZERO;
        //车队卡充值总计
        BigDecimal rechargeBalance = BigDecimal.ZERO;
        //车队卡总计赠送
        BigDecimal giftBalance = BigDecimal.ZERO;

        for(FleetCardRecordBalanceVo fleetCard : list){
            if(CardRechargeRecordEnum.MANUAL_SUB.getValue().equals(fleetCard.getCardType())
                    || CardRechargeRecordEnum.CONSUMPTION_XF.getValue().equals(fleetCard.getCardType())){
                consumptionBalance = consumptionBalance.add(Objects.nonNull(fleetCard.getRechargeAmount()) ? fleetCard.getRechargeAmount() : BigDecimal.ZERO);
            }
            if(CardRechargeRecordEnum.FLEET_RECHARGE.getValue().equals(fleetCard.getCardType())
                    || CardRechargeRecordEnum.MANUAL_ADD.getValue().equals(fleetCard.getCardType())){
                rechargeBalance = rechargeBalance.add(Objects.nonNull(fleetCard.getRechargeAmount()) ? fleetCard.getRechargeAmount() : BigDecimal.ZERO);
            }
            giftBalance = giftBalance.add(Objects.nonNull(fleetCard.getGiftAmount()) ? fleetCard.getGiftAmount() : BigDecimal.ZERO);
        }

        // 车队卡主卡余额总计 和 车队卡子卡卡余额总计 只受油企、油站 和车队名称 选择筛选
        //车队卡主卡余额总计
        BigDecimal parentCardNumberBalance = BigDecimal.ZERO;
        //车队卡子卡卡余额总计
        BigDecimal sonCardNumberBalance = BigDecimal.ZERO;

        FleetCardQueryForm fleetCardQueryForm = new FleetCardQueryForm();
        if(Objects.nonNull(fleetCardRechargeRecordDto)) {
            fleetCardQueryForm.setFleetCardId(fleetCardRechargeRecordDto.getFleetCardId());
            fleetCardQueryForm.setEnterpriseId(fleetCardRechargeRecordDto.getEnterpriseId());
            fleetCardQueryForm.setDel(fleetCardRechargeRecordDto.getDel());
            // 油企、油站 相同
            if(Objects.isNull(fleetCardRechargeRecordDto.getEnterpriseId()) && Objects.nonNull(fleetCardRechargeRecordDto.getStationId())){
                Station station = stationService.selectStationById(fleetCardRechargeRecordDto.getStationId());
                fleetCardQueryForm.setEnterpriseId(station.getEnterpriseId());
            }
        }
        log.info("fleetCardQueryForm 余额总计请求参数：[{}]",JSONObject.toJSONString(fleetCardQueryForm));
        List<FleetCardSub> fleetCardSubList = new ArrayList<>();
        List<FleetCardVo> fleetCardVoList = fleetCardService.selectFleetNameList(fleetCardQueryForm);
        for(FleetCardVo fleetCardVo : fleetCardVoList) {
            parentCardNumberBalance = parentCardNumberBalance.add(Objects.nonNull(fleetCardVo) ? fleetCardVo.getBalance() : BigDecimal.ZERO);

            List<FleetCardSub> fleetCardSubs = fleetCardSubService.selectFleetCardSubByIdAndSecondary(fleetCardVo.getId());
            for(FleetCardSub fleetCardSub : fleetCardSubs){
                if(!fleetCardSubList.contains(fleetCardSub)){
                    fleetCardSubList.add(fleetCardSub);
                    sonCardNumberBalance = sonCardNumberBalance.add(Objects.nonNull(fleetCardSub) ? fleetCardSub.getBalance() : BigDecimal.ZERO);
                }
            }
        }

        FleetCardRecordBalanceVo fleetCardRecordBalanceVo = new FleetCardRecordBalanceVo();
        fleetCardRecordBalanceVo.setConsumptionBalance(consumptionBalance);
        fleetCardRecordBalanceVo.setRechargeBalance(rechargeBalance);
        fleetCardRecordBalanceVo.setGiftBalance(giftBalance);
        fleetCardRecordBalanceVo.setParentCardNumberBalance(parentCardNumberBalance);
        fleetCardRecordBalanceVo.setSonCardNumberBalance(sonCardNumberBalance);

        return fleetCardRecordBalanceVo;
    }
}
